<?php 
function get_week(){
    $week = date('w');
    $start_date = date('Y-m-d',strtotime( '+'.(1-$week).' days'));
    $end_date = date('Y-m-d',strtotime('+'.(7-$week).' days' ));
    return array($start_date, $end_date);
}

function fmt_text($t){
    return nl2br(str_replace(" ", '&nbsp;', $t));
}

function get_month(){
    $start_date = date('Y-m-01');
    $end_date = date('Y-m-d', strtotime("$start_date +1 month -1 day"));
    return array($start_date,$end_date);
}

function fmt_date($time){
    return date("Y-m-d", $time);
}

function get_last_name($name){
    $str = mb_substr($name,-1,1,"UTF-8");
    return $str;
}

function fmt_hide($phone){
    $tmp = substr($phone, 4, 4);
    return str_replace($tmp, '****', $phone);
}

/**
 * 对象转数组
 * @param  [type] $obj [description]
 * @return [type]      [description]
 */
function objectToArray($obj){
    $arr = is_object($obj) ? get_object_vars($obj) : $obj;
    if(is_array($arr)){
        return array_map(__FUNCTION__, $arr);
    }else{
        return $arr;
    }
}


/**
 * 加密方法
**/
function _encrypt($data, $key, $expire = 0) {
   $key  = md5($key);
   $data = base64_encode($data);
   $x    = 0;
   $len  = strlen($data);
   $l    = strlen($key);
   $char =  '';
   for ($i = 0; $i < $len; $i++) {
      if ($x == $l) $x=0;
      $char  .= substr($key, $x, 1);
      $x++;
   }
   $str = sprintf('%010d', $expire ? $expire + time() : 0);
   for ($i = 0; $i < $len; $i++) {
      $str .= chr(ord(substr($data,$i,1)) + (ord(substr($char,$i,1)))%256);
   }
   $str = base64_encode($str);
   $str = str_replace(array('=','+','/'),array('O0O0O','o000o','oo00o'),$str);
   return $str;
}

/**
 * 解密方法
**/
function _decrypt($data, $key){
   $data = str_replace(array('O0O0O','o000o','oo00o'), array('=','+','/'),$data); 
   $key    = md5($key);
   $x      = 0;
   $data   = base64_decode($data);
   $expire = substr($data, 0, 10);
   $data   = substr($data, 10);
   if($expire > 0 && $expire < time()) {
      return null;
   }
   $len  = strlen($data);
   $l    = strlen($key);
   $char = $str = '';
   for ($i = 0; $i < $len; $i++) {
      if ($x == $l) $x = 0;
      $char  .= substr($key, $x, 1);
      $x++;
   }
   for ($i = 0; $i < $len; $i++) {
      if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
         $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
      }else{
         $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
      }
   }
   return base64_decode($str);
}
 ?>